const RequestMum = {
    async getMessageDetail(mumHost, secretKid, secretK, tenantCode, bizId, userGuid) {
        let url = mumHost + '/mum/v1/message/inquiry/user-messages';
        let headers = await this.getHeaders(secretKid, secretK, tenantCode);
        let data = {
            "tenantCode": tenantCode,
            "bizId": bizId,
            "userGuid": userGuid.toLowerCase(),
            "clientType": 1
        };
        let curl = Utils.generateCurl('POST', url, JSON.stringify(data), headers);
        console.log('user-messages CURL:');
        console.log(curl);
        let message1 = await new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: url,
                data: JSON.stringify(data),
                headers: headers,
                onload: function (response) {
                    const jsonData = JSON.parse(response.responseText);
                    console.log(jsonData);
                    resolve(jsonData);
                },
                onerror: function (error) {
                    reject(new Error('请求失败: ' + error.statusText));
                },
                ontimeout: function () {
                    reject(new Error('请求超时'));
                }
            });
        });

        data.clientType = 2; // 修改 clientType 为 2
        let message2 = await new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: url,
                data: JSON.stringify(data),
                headers: headers,
                onload: function (response) {
                    const jsonData = JSON.parse(response.responseText);
                    console.log(jsonData);
                    resolve(jsonData);
                },
                onerror: function (error) {
                    reject(new Error('请求失败: ' + error.statusText));
                },
                ontimeout: function () {
                    reject(new Error('请求超时'));
                }
            });
        });

        url = mumHost + '/mum/v1/message/inquiry/user-message-details';
        let messageExt = await new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: url,
                data: JSON.stringify(data),
                headers: headers,
                onload: function (response) {
                    const jsonData = JSON.parse(response.responseText);
                    console.log(jsonData);
                    resolve(jsonData);
                },
                onerror: function (error) {
                    reject(new Error('请求失败: ' + error.statusText));
                },
                ontimeout: function () {
                    reject(new Error('请求超时'));
                }
            });
        });

        let appMessage = message1.data.length > 0 ? message1.data[0] : {};
        let pcMessage = message2.data.length > 0 ? message2.data[0] : {};

        for (let i = 0; i < messageExt.data.length; i++) {
            let item = messageExt.data[i];
            if (item.msg_channel === 1) {
                for (let key in item) {
                    appMessage[key] = item[key];
                }
            } else if (item.msg_channel === 2) {
                for (let key in item) {
                    pcMessage[key] = item[key];
                }
            }
        }
        return {appMessage, pcMessage};
    },
    async getHeaders(secretKid, secretK, tenantCode) {
        let token = await EmcConfig.getMyAuthToken(secretKid, secretK);
        return {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'tenantCode': tenantCode,
            'my-api-Authorization': token
        }
    },
    postMum(url, headers, data) {
        let curl = Utils.generateCurl('POST', url, JSON.stringify(data), headers);
        console.log('CURL postMum:');
        console.log(curl);
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: url,
                data: JSON.stringify(data),
                headers: headers,
                onload: function (response) {
                    const jsonData = JSON.parse(response.responseText);
                    resolve(jsonData);
                },
                onerror: function (error) {
                    reject(new Error('请求失败: ' + error.statusText));
                },
                ontimeout: function () {
                    reject(new Error('请求超时'));
                }
            });
        });
    },
};